//
//  MainViewController.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/5/28.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TT_TabBarView.h"
#import "MainViewController.h"

#import "HomeViewController.h"
#import "CategoryViewController.h"
#import "MineViewController.h"


@interface MainViewController ()
{
    NSInteger _currentIndex;
}
@property (nonatomic ,strong) TT_TabBarView *tabbarView;

@property (nonatomic, strong) UIViewController *ARVC;
@property (nonatomic, strong) MineViewController *mineVC;
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, strong) CategoryViewController *categoryVC;

@property (nonatomic, strong)UIViewController *selectVC;

@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) NSMutableArray <NSMutableDictionary *>*ARModelArray;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestARModels];
    [self initView];
    _currentIndex = -1;

    [self setUpViewControllers];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarButtonChangeNotification:) name:tabbarItemSelectNoti object:nil];

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)tabBarButtonChangeNotification:(NSNotification *)notification{
    
    //获取通知的值
    NSInteger index = [[notification.userInfo objectForKey:@"action"] integerValue];
    //切换控制器
    [self changeViewController:index];
}

- (void)setUpViewControllers {
    
    [self addChildViewController:self.homeVC];
    [self addChildViewController:self.categoryVC];
    [self addChildViewController:self.ARVC];
    [self addChildViewController:self.mineVC];
    [self changeViewController:0];
}

- (void)changeViewController:(NSInteger)index {
    
    if (_currentIndex != index) {
        if (index != 2) {
            [self.selectVC.view removeFromSuperview];
            //获取新的
            UIViewController *newVC = self.childViewControllers[index];
            //属性更换成新的视图
            self.selectVC = newVC;
            //添加新的view到主视图
            [self.view addSubview:newVC.view];
            //设置约束
            [newVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.bottom.equalTo(self.view);
                make.top.mas_equalTo([TT_SizeHelper getGlobalStatusBarHeight]);
                make.left.equalTo(self.tabbarView.mas_right);
            }];
            
            _currentIndex = index;
        } else {
            [self goAR];
        }
        
    } else {
        
        return;
    }

    
}

- (void)initView {
    
    self.view.backgroundColor = UIColorHex(0x202024);
    [self.view addSubview:self.tabbarView];
    
    [self.tabbarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.top.mas_equalTo([TT_SizeHelper getGlobalStatusBarHeight]);
        make.width.mas_equalTo(tabbarWidth);
    }];
}

- (void)requestARModels {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_ARMODELS_API];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:@(self.pageSize) forKey:@"pageSize"];
    [param setValue:@(self.pageNum) forKey:@"pageNum"];
    
    WEAKSELF
    [TTHttpManager getRequestWithUrl:url TimeOut:10.0 GetData:param FinishBlock:^(id  _Nonnull data) {
        
        BOOL fail = [[data valueForKey:@"code"] boolValue];
        NSDictionary *resultDic = [data valueForKey:@"data"];
        if (!fail) {
            weakSelf.ARModelArray = [resultDic valueForKey:@"result"];
            
        } else {
            
            NSString *message = [resultDic valueForKey:@"message"];
            if (message.length) {
                
            } else {
                message = @"未获取到AR模型，且服务器未返回message";
            }
            [WHToast showErrorWithMessage:[resultDic valueForKey:@"message"] duration:TT_GLOBAL_DURATION_TIME finishHandler:^{
                
            }];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
    
}

- (UIView *)tabbarView {
    if (!_tabbarView) {
        _tabbarView = [[TT_TabBarView alloc] init];
        _tabbarView.backgroundColor = UIColorHex(0x202024);
    }
    return _tabbarView;
}

- (HomeViewController *)homeVC {
    if (!_homeVC) {
        _homeVC = [[HomeViewController alloc] init];
    }
    return _homeVC;
}

- (UIViewController *)ARVC {
    if (!_ARVC) {
        _ARVC = [[UIViewController alloc] init];
    }
    return _ARVC;
}

- (CategoryViewController *)categoryVC {
    if (!_categoryVC) {
        _categoryVC = [[CategoryViewController alloc] init];
    }
    return _categoryVC;
}

-(MineViewController *)mineVC {
    if (!_mineVC) {
        _mineVC = [[MineViewController alloc] init];
    }
    return _mineVC;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self setStatusBarBackgroundColor:UIColorHex(0x202024)];
}

- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}


- (void)goAR {
#if TARGET_IPHONE_SIMULATOR//模拟器
    
#elif TARGET_OS_IPHONE//真机

//    NSString *jsonStr = @"[{\"modelId\":\"0\",\"modelName\":\"沙发\",\"modelDownloadUrl\":\"http://test.qiniu.krpano100.com/ar/model/model.zip?attname=\",\"modelZipPath\":\"model.scnassets/model.obj\",\"modelCoverUrl\":\"http://test.qiniu.krpano100.com/ar/model/cover/model.png\"},{\"modelId\":\"1\",\"modelName\":\"凳子\",\"modelDownloadUrl\":\"http://test.qiniu.krpano100.com/ar/model/model1.zip?attname=\",\"modelZipPath\":\"model1.scnassets/model1.obj\",\"modelCoverUrl\":\"http://test.qiniu.krpano100.com/ar/model/cover/mode1.png\"}]";
//
//
//    NSData *json = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
//    NSMutableArray<NSMutableDictionary *> *asd = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil];
    // Do any additional setup after loading the view, typically from a nib.
    Ar100Maganer *manager = [Ar100Maganer new];
    self.ar100Maganer = manager;
    [self.ar100Maganer init:self.ARModelArray decryptType:0 decryptKey:@""];
    self.ar100Maganer.allModels = self.ARModelArray;
    
    
    NSMutableArray<NSString *> *relatedIds = [NSMutableArray<NSString *> new];
    [relatedIds addObject:@"2"];
    [self.ar100Maganer showById:self modelId:@"1" relatedIds:relatedIds];
    
    
    
#endif
    

}


@end
